import Vue from 'vue';
import Router from 'vue-router';
import storage from '@/model/storage'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.use(Router);

const routes = [
  {
    path: '/',
    redirect: '/tabmain'
  },
  {
    path:'/tabmain',
    name: 'tabmain',
    component: () => import('./view/tabmain'),
    meta: {
      title: '首页',
      requireAuth:false,
    } 
  },
  {
    path:'/taborder',
    name: 'taborder',
    component: () => import('./view/taborder'),
    meta: {
      title: '订单查询',
      requireAuth:true,
    } 
  },
  {
    path:'/tabexpress',
    name: 'tabexpress',
    component: () => import('./view/tabexpress'),
    meta: {
      title: '包裹查询',
      requireAuth:true,
    } 
  },
  {
    path:'/tabuser',
    name: 'tabuser',
    component: () => import('./view/tabuser'),
    meta: {
      title: '我的',
      requireAuth:true,
    } 
  },
  {
    path:'/login',
    name: 'login',
    component: () => import('./view/login'),
    meta: {
      title: '登录',
      requireAuth:false,
    } 
  },
  {
    path:'/forecast',
    name: 'forecast',
    component: () => import('./view/forecast'),
    meta: {
      title: '运费估算',
      requireAuth:true,
    }
  },
  {
    path:'/atricle',
    name: 'atricle',
    component: () => import('./view/atricle'),
    meta: {
      title: '资讯详情',
      requireAuth:false,
    }
  },
  {
    path:'/createorder',
    name: 'createorder',
    component: () => import('./view/createorder'),
    meta: {
      title: '快速寄件',
      requireAuth:true,
    }
  },
  {
    path:'/claim',
    name: 'claim',
    component: () => import('./view/claim'),
    meta: {
      title: '快递认领',
      requireAuth:true,
    }
  },
  {
    path:'/search',
    name: 'search',
    component: () => import('./view/search'),
    meta: {
      title: '搜索',
      requireAuth:false,
    }
  },
  {
    path:'/orderinfo',
    name: 'orderinfo',
    component: () => import('./view/orderinfo'),
    meta: {
      title: '订单详情',
      requireAuth:true,
    }
  },
  {
    path:'/editorder',
    name: 'editorder',
    component: () => import('./view/editorder'),
    meta: {
      title: '修改订单',
      requireAuth:true,
    }
  },
  {
    path:'/editpaypwd',
    name: 'editpaypwd',
    component: () => import('./view/editpaypwd'),
    meta: {
      title: '支付密码',
      requireAuth:true,
    }
  },
  {
    path:'/mywallet',
    name: 'mywallet',
    component: () => import('./view/mywallet'),
    meta: {
      title: '我的钱包',
      requireAuth:true,
    }
  },
  {
    path:'/mywalletmx',
    name: 'mywalletmx',
    component: () => import('./view/mywalletmx'),
    meta: {
      title: '钱包明细',
      requireAuth:true,
    }
  },
  {
    path:'/mywalletcog',
    name: 'mywalletcog',
    component: () => import('./view/mywalletcog'),
    meta: {
      title: '余额充值',
      requireAuth:true,
    }
  },
  {
    path:'/jifen',
    name: 'jifen',
    component: () => import('./view/jifen'),
    meta: {
      title: '我的积分',
      requireAuth:true,
    }
  },
  {
    path:'/jifenchange',
    name: 'jifenchange',
    component: () => import('./view/jifenchange'),
    meta: {
      title: '积分转余额',
      requireAuth:true,
    }
  },
  {
    path:'/about',
    name: 'about',
    component: () => import('./view/about'),
    meta: {
      title: '关于我们',
      requireAuth:true,
    }
  },
  {
    path:'/feedback',
    name: 'feedback',
    component: () => import('./view/feedback'),
    meta: {
      title: '意见反馈',
      requireAuth:true,
    }
  },
  {
    path:'/myaddress',
    name: 'myaddress',
    component: () => import('./view/myaddress'),
    meta: {
      title: '收货地址',
      requireAuth:true,
    }
  },
  {
    path:'/myaddressadd',
    name: 'myaddressadd',
    component: () => import('./view/myaddressadd'),
    meta: {
      title: '新增地址',
      requireAuth:true,
    }
  },
  {
    path:'/expressadd',
    name: 'expressadd',
    component: () => import('./view/expressadd'),
    meta: {
      title: '新增包裹',
      requireAuth:true,
    }
  },
  {
    path:'/userinfo',
    name: 'userinfo',
    component: () => import('./view/userinfo'),
    meta: {
      title: '个人资料',
      requireAuth:true,
    }
  },
];

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '');
});

const router = new Router({ routes ,mode:"history"}); //  

router.beforeEach((to, from, next) => {
  NProgress.start();
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = title;
  }
  if(to.meta.requireAuth){
    var userToken  = storage.get('userToken');
    if(userToken == null || !userToken){
      storage.set('loginPath',to.path == "/login" ? "/tabmain" : to.path); //记录要去的页面
      next('/login');
      NProgress.done()
    }else{
      var currentPaths = storage.sessionGet('historyList') || [];
      currentPaths.push(to.path == "/login" ? "/login" : to.path);
      storage.sessionSet('historyList',currentPaths);
      next();
    }
  }else{
    next();
  }
});
router.afterEach(() => {
  NProgress.done()
})
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}
export {
  router,
};
